import request from "@/api";
import { BaseRet, PageQuery, PageRet } from "../../response";

export default class EmailTemplateApi {
  /**
   * 查询
   * @returns
   */
  static async query(data: EmailTemplateQuery): Promise<PageRet<EmailTemplateModel>> {
    return await request({
      url: "/emailTemplate/query",
      method: "post",
      data: data,
    });
  }

  /**
   * 新增
   * @returns
   */
  static async create(data: EmailTemplateModel): Promise<BaseRet> {
    return await request({
      url: `/emailTemplate/create`,
      method: "post",
      data: data,
    });
  }

  /**
   * 修改
   * @returns
   */
  static async update(data: EmailTemplateModel): Promise<BaseRet> {
    return await request({
      url: `/emailTemplate/update`,
      method: "put",
      data: data,
    });
  }

  /**
   * 删除
   * @returns
   */
  static async delete(id: string): Promise<BaseRet> {
    return await request({
      url: `/emailTemplate/delete/${id}`,
      method: "delete",
    });
  }

  /**
   * 查询模板变量类
   * @returns
   */
  static async queryVariableClass(): Promise<TemplateVariableModel[]> {
    return await request({
      url: `/emailTemplate/QueryVariableClass`,
      method: "get",
    });
  }

  /**
   * 查询模板变量
   * @returns
   */
  static async queryVariable(value: string): Promise<TemplateVariableModel[]> {
    return await request({
      url: `/emailTemplate/QueryVariable?variableClass=${value}`,
      method: "get",
    });
  }

  /**
   * 查询模板变量
   * @returns
   */
  static async queryDefaultJson(value: string): Promise<TemplateVariableJson> {
    return await request({
      url: `/emailTemplate/QueryDefaultJson?variableClass=${value}`,
      method: "get",
    });
  }

  /**
   * 测试发送邮件
   * @returns
   */
  static async testSendEmail(data: TestSendEmailParam): Promise<BaseRet> {
    return await request({
      url: `/emailTemplate/TestSendEmail`,
      method: "post",
      data: data,
    });
  }

  /**
   * 导出
   * @returns
   */
  static async exportExcel() {
    return await request({
      url: `/emailTemplate/ExportExcel`,
      responseType: "arraybuffer",
      method: "get",
    });
  }

  /**
   * 导入
   * @returns
   */
  static async importExcel(file: any): Promise<BaseRet> {
    return await request({
      url: `/emailTemplate/ImportExcel`,
      headers: { "Content-Type": "multipart/form-data" },
      method: "post",
      data: { file: file },
    });
  }
}

export interface EmailTemplateModel {
  id: string;
  name: string;
  bccEmailAddresses: string;
  subject: string;
  body: string;
  isActive: boolean;
  emailAccountId: string;
  email: string;
  variableClass: string;
}

export interface EmailTemplateQuery extends PageQuery {
  searchString: string;
}

export interface EmailTemplateEdit {
  id?: string;
  name?: string;
  bccEmailAddresses?: string;
  subject?: string;
  body?: string;
  isActive?: boolean;
  emailAccountId?: string;
  variableClass: string;
}

export interface TemplateVariableModel {
  text?: string;
  value?: string;
}

export interface TemplateVariableJson {
  json?: string;
}

export interface TestSendEmailParam {
  id?: string;
  emailData?: string;
  toEmailAddress?: string;
  toEmailName?: string;
}
